package com.eds.controller;

import com.eds.domain.CourseDocument;
import com.eds.domain.CourseSearchDTO;
import com.eds.pojo.BaseResult;
import com.eds.pojo.CodeEnum;
import com.eds.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName：SearchController
 * @Author: vvSmile
 * @Date: 2025/04/11
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;


    //自动补全
    @GetMapping("/autoSuggest")
    public BaseResult autoSuggest(String keyword) throws IOException {
        List<String> list = searchService.autoSuggest(keyword);
        if (list != null) {
            return BaseResult.ok("自动补全成功！", list);
        } else
            return BaseResult.ok("自动补全为空");
    }


    //搜索课程
    @PostMapping("/searchCourse")
    public BaseResult searchCourse(@RequestBody CourseSearchDTO courseSearchDTO) {
        PageImpl<CourseDocument> list = searchService.searchCourse(courseSearchDTO);
        if (list != null) {
            return BaseResult.ok("搜索课程成功！", list);
        } else
            return BaseResult.error(CodeEnum.SEARCH_COURSE_ERROR);
    }
}
